package 内存结构.方法区.StringTable;
// StringTable [ "a", "b" ,"ab" ]  串池   hashtable 结构，不能扩容
public class StringTable {
    // 常量池中的信息，都会被加载到运行时常量池中， 这时 a b ab 都是常量池中的符号，还没有变为 java 字符串对象
    // ldc #2 会把 a 符号变为 "a" 字符串对象
    // ldc #3 会把 b 符号变为 "b" 字符串对象
    // ldc #4 会把 ab 符号变为 "ab" 字符串对象

    public static void main(String[] args) {
        String s1 = "a";
        String s2 = "b";
        String s3 = "ab";
        String s4 = s1 + s2; // new StringBuilder().append("a").append("b").toString()  new String("ab")
        //StringTable中存储的并不是String类型的对象，储存的而是指向String对象的索引，真实对象还是储存在堆中。
        // s3指向的对象是new String("ab")
        // s4指向的对象是new String("ab")
        // s3和s4属于两个对象
        String s5 = "a" + "b";  // javac 在编译期间的优化，结果已经在编译期确定为ab

        /*
        String s1 = "a";
             0: ldc           #2                  // String a
             2: astore_1  存到LocalVariableTable:Slot为1
        String s2 = "b";
             3: ldc           #3                  // String b
             5: astore_2  存到LocalVariableTable:Slot为2
        String s3 = "ab";
             6: ldc           #4                  // String ab
             8: astore_3
        String s4 = s1 + s2;
             9: new           #5                  // class java/lang/StringBuilder
            12: dup
            13: invokespecial #6                  // Method java/lang/StringBuilder."<init>":()V  调用无惨构造init
            16: aload_1  读取LocalVariableTable:Slot为1
            17: invokevirtual #7                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
            20: aload_2
            21: invokevirtual #7                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
            24: invokevirtual #8                  // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
            27: astore        4
        String s5 = "a" + "b"
            29: ldc           #4                  // String ab
            31: astore        5


      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      34     0  args   [Ljava/lang/String;
            3      31     1    s1   Ljava/lang/String;
            6      28     2    s2   Ljava/lang/String;
            9      25     3    s3   Ljava/lang/String;
           29       5     4    s4   Ljava/lang/String;
           33       1     5    s5   Ljava/lang/String;

         */


    }
}
